** PREPARING THE BES DATA FOR ANALYSIS

clear all	
cd "${data}"	


clear all	 
set maxvar 10000

use "BES2019_W23_Panel_v23.0.dta", clear 


keep wave* id partyIdW* age* p_edlevelW* gender leftRightW* lrLabW* lrUKIPW* lrConW* lrLDW* lrBrexitW* ageW* gender polAttentionW* partyContact1W* partyContactConW* partyContactLDW* partyContactLabW* infoSourceInternetW* infoSourcePaperW* infoSourceRadioW* infoSourceTVW* partyIdSqueezeW* respdateW5 respdateW12 respdateW18 CampaignDayW5 CampaignDayW12 CampaignDayW18

rename ageW* age#, renumber
rename p_edlevelW* education#, renumber
rename gender sex
rename polAttentionW* attention#, renumber
rename partyContact1W* contactedbyaparty#, renumber
rename partyContactConW* contactedbycon#, renumber 
rename partyContactLDW* contactedbyld#, renumber
rename partyContactLabW* contactedbylab#, renumber 
rename infoSourceInternetW* politics_internet#, renumber
rename infoSourcePaperW* politics_newspaper#, renumber
rename infoSourceRadioW* politics_radio#, renumber
rename infoSourceTVW* politics_TV#, renumber



*POLITICAL ATTENTION

forval i=1/19 {
replace attention`i' = . if attention`i' > 10
}


*** DK Labour Party Position


forval i=2/12 {
gen dk_Labour`i'=.
}

forval i=2/12 {
replace dk_Labour`i' = 1 if lrLabW`i' == 9999
replace dk_Labour`i' = 0 if lrLabW`i' >= 0 & lrLabW`i' <= 10
}
 

*** DK Conservative  Party Position

forval i=2/12 {
gen dk_Con`i'=.
}

forval i=2/12 {
replace dk_Con`i' = 1 if lrConW`i' == 9999
replace dk_Con`i' = 0 if lrConW`i' >= 0 & lrConW`i' <= 10

}

*** DK LIBDEM Position


forval i=2/12 {
gen dk_LD`i'=.
}

forval i=2/12 {
replace dk_LD`i' = 1 if lrLDW`i' == 9999
replace dk_LD`i' = 0 if lrLDW`i' >= 0 & lrLDW`i' <= 10
}

*** DK UKIP Position

forval i=2/12 {
gen dk_UKIP`i'=.
}

forval i=2/12 {
replace dk_UKIP`i' = 1 if lrUKIPW`i' == 9999
replace dk_UKIP`i' = 0 if lrUKIPW`i' >= 0 & lrUKIPW`i' <= 10
}


*** Perception of Labour Party Position

forval i=2/12 {
replace lrLabW`i' = . if lrLabW`i' > 10
}

forval i=15/18 {
replace lrLabW`i' = . if lrLabW`i' > 10
}

forval i=20/23 {
replace lrLabW`i' = . if lrLabW`i' > 10
}


*** Perception of Conservative  Party Position

forval i=2/12 {
replace lrConW`i' = . if lrConW`i' > 10
}

forval i=15/18 {
replace lrConW`i' = . if lrConW`i' > 10
}

forval i=20/23 {
replace lrConW`i' = . if lrConW`i' > 10
}



*** Perception of LIBDEM Position

forval i=2/12 {
replace lrLDW`i' = . if lrLDW`i' > 10
}

forval i=15/18 {
replace lrLDW`i' = . if lrLDW`i' > 10
}

forval i=20/23 {
replace lrLDW`i' = . if lrLDW`i' > 10
}


*** Perception of UKIP Position

forval i=2/12 {
replace lrUKIPW`i' = . if lrUKIPW`i' > 10
}

forval i=15/16 {
replace lrUKIPW`i' = . if lrUKIPW`i' > 10
}


*** Perception of BREXIT PARTY Position

forval i=16/18 {
replace lrBrexitW`i' = . if lrBrexitW`i' > 10
}

forval i=20/23 {
replace lrBrexitW`i' = . if lrBrexitW`i' > 10
}


*** SELF-PLACEMENT 

*forval i=1/23 {
*replace leftRightW`i' = . if leftRightW`i' > 10
*}



*** PARTY IDENTIFICATION 

foreach i in 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 {
	gen pid`i' = partyIdW`i'
}

forval i=1/4 {
replace pid`i' = 9 if pid`i' == 4 | pid`i' == 5 | pid`i' == 7 | pid`i' == 8 
}		// other party category

forval i=6/23 {
replace pid`i' = 9 if pid`i' == 4 | pid`i' == 5 | pid`i' == 7 | pid`i' == 8 
}		// other party category

forval i=16/23 {
replace pid`i' = 9 if pid`i' == 12  
}		// Brexit Party moved to other party category

replace pid16 = 9 if pid16==11 // Change UK - The independent group moved to other. 

forval i=1/4 {
replace pid`i' = 4 if pid`i' == 6
}		// ukip is 4 now

forval i=6/23 {
replace pid`i' = 4 if pid`i' == 6
}		// ukip is 4 now


forval i=1/4 {
replace pid`i' = 5 if pid`i' == 9
}	//other party category is now 5

forval i=6/23 {
replace pid`i' = 5 if pid`i' == 9
}	//other party category is now 5


forval i=1/4 {
replace pid`i' = 0 if pid`i' == 10 | pid`i' == 9999
}		// no pid and dk are 0

forval i=6/23 {
replace pid`i' = 0 if pid`i' == 10 | pid`i' == 9999
}		// no pid and dk are 0



label define parties 0"independent" 1"Conservative Party" 2"Labour Party" 3"Liberal Democrats" 4"UKIP"  5"other"

forval i=1/4 {
label values pid`i' parties
}

forval i=6/23 {
label values pid`i' parties
}



forval i=1/23 {
	gen extremity`i' = .
}

forval i=1/23 {
replace extremity`i' = 0 if leftRightW`i' == 5
replace extremity`i' = 1 if leftRightW`i' == 6 | leftRightW`i' == 4 
replace extremity`i' = 2 if leftRightW`i' == 7 | leftRightW`i' == 3 
replace extremity`i' = 3 if leftRightW`i' == 8 | leftRightW`i' == 2
replace extremity`i' = 4 if leftRightW`i' == 9 | leftRightW`i' == 1
replace extremity`i' = 5 if leftRightW`i' == 10 | leftRightW`i' == 0

}	



** midscale Labour Party Position


forval i=2/12 {
gen ms_Labour`i'=.
}

forval i=2/12 {
replace ms_Labour`i' = 1 if lrLabW`i' == 5
replace ms_Labour`i' = 0 if lrLabW`i' == 0 | lrLabW`i' == 1 | lrLabW`i' == 2 | lrLabW`i' == 3 | lrLabW`i' == 4 | lrLabW`i' == 6 | lrLabW`i' == 7 | lrLabW`i' == 8 | lrLabW`i' == 9 | lrLabW`i' == 10
}
 
** midscale Con Party Position


forval i=2/12 {
gen ms_Con`i'=.
}

forval i=2/12 {
replace ms_Con`i' = 1 if lrConW`i' == 5
replace ms_Con`i' = 0 if lrConW`i' == 0 | lrConW`i' == 1 | lrConW`i' == 2 | lrConW`i' == 3 | lrConW`i' == 4 | lrConW`i' == 6 | lrConW`i' == 7 | lrConW`i' == 8 | lrConW`i' == 9 | lrConW`i' == 10
}
 

** midscale LD  Position


forval i=2/12 {
gen ms_LD`i'=.
}

forval i=2/12 {
replace ms_LD`i' = 1 if lrLDW`i' == 5
replace ms_LD`i' = 0 if lrLDW`i' == 0 | lrLDW`i' == 1 | lrLDW`i' == 2 | lrLDW`i' == 3 | lrLDW`i' == 4 | lrLDW`i' == 6 | lrLDW`i' == 7 | lrLDW`i' == 8 | lrLDW`i' == 9 | lrLDW`i' == 10
}

** midscale UKIP Position


forval i=2/12 {
gen ms_UKIP`i'=.
}

forval i=2/12 {
replace ms_UKIP`i' = 1 if lrUKIPW`i' == 5
replace ms_UKIP`i' = 0 if lrUKIPW`i' == 0 | lrUKIPW`i' == 1 | lrUKIPW`i' == 2 | lrUKIPW`i' == 3 | lrUKIPW`i' == 4 | lrUKIPW`i' == 6 | lrUKIPW`i' == 7 | lrUKIPW`i' == 8 | lrUKIPW`i' == 9 | lrUKIPW`i' == 10
}



drop if id==.
reshape long partyIdW age p_edlevelW leftRightW lrLabW lrConW lrLDW lrUKIPW lrBrexitW ageW gender polAttentionW contactedbyaparty contactedbycon contactedbyld contactedbylab politics_TV politics_radio politics_newspaper politics_internet dk_Labour dk_Con dk_LD dk_UKIPW partyIdSqueezeW ms_Labour ms_Con ms_LD ms_UKIP, i(id) j(vague)
		
/*		according to CSES experts 2015 
gen CSES_lr_Con = 7 
gen	CSES_lr_Lab = 4
gen CSES_lr_LD = 5
*/	

replace lrConW = . if lrConW==9999
replace lrLabW = . if lrLabW==9999
replace lrLDW = . if lrLDW==9999
replace lrBrexitW = . if lrBrexitW==9999
replace lrUKIPW = . if lrUKIPW==9999

replace politics_TV = . if politics_TV == 9999 // 1.none 2.less than 30 min 3. between 30-60 min 4. 1 to 2 hours 5. more than 2 hours
replace politics_radio = . if politics_radio == 9999
replace politics_newspaper = . if politics_newspaper == 9999
replace politics_internet = . if politics_internet == 9999

recode politics_TV (1=0) (2=1) (3=2) (4=3) (5=4)
recode politics_radio (1=0) (2=1) (3=2) (4=3) (5=4)
recode politics_newspaper (1=0) (2=1) (3=2) (4=3) (5=4)
recode politics_internet (1=0) (2=1) (3=2) (4=3) (5=4)

egen consumptionindex = rowmean (politics_TV politics_radio politics_newspaper politics_internet)

gen accuracy_Con = abs(7 - lrConW)
gen accuracy_Lab = abs(4 - lrLabW) // same as BES experts (3.9)
gen accuracy_LD = abs(5 - lrLDW) // same as BES experts (5.2)
gen accuracy_UKIP = abs(9 - lrUKIPW) // same as BES experts (8.7)
egen accuracy_index =rowmean(accuracy_Con accuracy_Lab accuracy_LD accuracy_UKIP)

gen accuracy_Con_2017 = abs(8 - lrConW) // BES experts (7.5)
gen accuracy_Lab_2017 = abs(2 - lrLabW) // BES experts (2.4)
gen accuracy_LD_2017 = abs(5 - lrLDW) // BES experts (4.6)
gen accuracy_UKIP_2017 = abs(9 - lrUKIPW) // BES experts (8.8)
egen accuracy_index_2017 =rowmean(accuracy_Con accuracy_Lab accuracy_LD accuracy_UKIP)

gen accuracy_Con_2019 = abs(8 - lrConW) // BES experts (7.5)
gen accuracy_Lab_2019 = abs(2 - lrLabW) // BES experts (2.4)
gen accuracy_LD_2019 = abs(5 - lrLDW) // BES experts (4.6)
gen accuracy_Brexit_2019 = abs(9 - lrBrexitW) // BES experts (8.8)
egen accuracy_index_2019 =rowmean(accuracy_Con accuracy_Lab accuracy_LD accuracy_UKIP)



egen overall_dk =rowmean(dk_Labour dk_Con dk_LD dk_UKIPW)
egen overall_ms =rowmean(ms_Labour ms_Con ms_LD ms_UKIP)

gen wave_ = vague 
lab define wavedates 1 "Feb/Mar'14" 2 "May/Jun'14" 3 "Sept/Oct'14" 4 "Mar'15" 5 "Apr/May'15" 6 "May '15" ///
7 "Apr/May'16" 8 "May/Jun'16" 9 "Jun/Jul'16" 10 "Nov/Dec'16" 11 "Apr/May'17" 12 "May/Jun'17" 13 "Jun'17" ///
14 "May '18" 15"Mar/May'19" 16"May/Jun'19" 17"Nov'19" 18"Nov/Dec'19" 19"Dec'19" 20"Jun'20" 21"May'21"
	
		label values wave_ wavedates 		
		
	
gen day_w5 = day(respdateW5)
gen month_w5 = month(respdateW5)	
	
	
gen day_w12 = day(respdateW12)
gen month_w12 = month(respdateW12)		

gen day_w18 = day(respdateW18)
gen month_w18 = month(respdateW18)		
	
save "data2.dta", replace
